<template>
	<view class="loveDetail">

		<img src="/static/love_bg2.jpg" style="position: absolute; top: 0; left: 0;width:100%;height:100vh;" />
		<view class="" style="height: 100vh;overflow: scroll;">
			<swiper class="card-swiper square-dot" :indicator-dots="true" :circular="true" :autoplay="true" interval="5000"
				duration="500" @change="cardSwiper" indicator-color="#8799a3" indicator-active-color="#0081ff">
				<swiper-item v-for="(item,index) in info.photos" :key="index" :class="cardCur==index?'cur':''">
					<view class="swiper-item" @tap="previewImage(index)">
						<image :src="item" mode="aspectFit"></image>
					</view>
				</swiper-item>
			</swiper>
			<view class="cu-list menu card-menu margin-top">
				<view class="cu-item">
					<view class="content">
						<text class="cuIcon-title text-orange"></text>
						昵称: <text class="text-grey padding-left">{{info.nick_name}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="cuIcon-title text-orange"></text>
						性别: <text class="text-grey padding-left">{{info.gender}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="cuIcon-title text-orange"></text>
						年龄: <text class="text-grey padding-left">{{calculateAge(info.birthday)}}岁</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="cuIcon-title text-orange"></text>
						所在位置: <text class="text-grey padding-left" v-if="info.region">
							{{info.region[0]}} {{info.region[1]}} {{info.region[2]}}
						</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="cuIcon-title text-orange"></text>
						身高: <text class="text-grey padding-left">{{info.height}}cm</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="cuIcon-title text-orange"></text>
						体重: <text class="text-grey padding-left">{{info.weight}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="cuIcon-title text-orange"></text>
						学历: <text class="text-grey padding-left">{{info.degree}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="cuIcon-title text-orange"></text>
						收入: <text class="text-grey padding-left">{{info.income}}</text>
					</view>
				</view>
				<view class="cu-item" v-if="invite_status == 1 || other_invite_status == 1">
					<view class="content">
						<text class="cuIcon-title text-orange"></text>
						微信号: <text class="text-grey padding-left">{{info.wechat_id}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="cuIcon-title text-orange"></text>
						性格: <text class="text-grey padding-left">{{info.character}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="cuIcon-title text-orange"></text>
						爱好: <text class="text-grey padding-left">{{info.hobby}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content" style="display: flex;">
						<text class="cuIcon-title text-orange"></text>
						<view style="display: flex; align-items: center; justify-content: space-between; width: 100%;">
							对另一半期望:<uv-button text="查看" size="small"
								color="linear-gradient(to right, rgb(238, 228, 238), rgb(219, 221, 242))"
								:customTextStyle="{color:'#8799a3'}" @click="toCheck"></uv-button>
						</view>

					</view>
				</view>
				<view class="padding flex flex-direction" v-if="invite_status == -1 && other_invite_status == -1">
					<button class="cu-btn bg-red margin-tb-sm lg" @tap="inviteContact()">邀请联系</button>
				</view>
				<view class="padding flex flex-direction" v-if="invite_status >= 0">
					<button class="cu-btn bg-red margin-tb-sm lg" v-if="invite_status == 0">等待对方同意</button>
					<button class="cu-btn bg-green margin-tb-sm lg" v-if="invite_status == 1">对方已同意</button>
					<button class="cu-btn bg-grey margin-tb-sm lg" v-if="invite_status == 2">对方已拒绝</button>
				</view>
				<view class="padding flex flex-direction" v-if="other_invite_status >= 0">
					<button class="cu-btn bg-orange margin-tb-sm lg" v-if="other_invite_status == 0"
						@tap="acceptContact()">接受邀请</button>
					<button class="cu-btn bg-red margin-tb-sm lg" v-if="other_invite_status == 0"
						@tap="refuseContact()">拒绝邀请</button>
					<button class="cu-btn bg-green margin-tb-sm lg" v-if="other_invite_status == 1">你已同意</button>
					<button class="cu-btn bg-grey margin-tb-sm lg" v-if="other_invite_status == 2">你已拒绝</button>
				</view>
			</view>
			<view class="cu-modal" catchtouchmove='true' :class="modalName=='Image'?'show':''" @tap="hideModal">
				<view class="cu-dialog" @tap.stop="">
					<image :src="modalImg" mode="widthFix"></image>
				</view>
			</view>


			<uv-popup ref="popup" @change="handleChange" round="20">
				<view class="popup">
					<uv-text text="我期待的她/他" size="20" color="#f37b1d" margin="0 0 20px"></uv-text>
					<uv-cell-group :border="false">

						<uv-cell title="性别" :value="info.want_gender"></uv-cell>
						<uv-cell title="年份" :value="info.want_year + '年'"></uv-cell>
						<uv-cell title="所在地区" v-if="info.want_region && info.want_region.length>0"
							:value="info.want_region[0] + info.want_region[1] + info.want_region[2]"></uv-cell>
						<uv-cell title="身高" :value="info.want_height +'cm' "></uv-cell>
						<uv-cell title="体重" :value="info.want_weight + 'kg' "></uv-cell>
						<uv-cell title="学历" :value="info.want_degree"></uv-cell>
						<uv-cell title="收入(年薪)" :value="info.want_income"></uv-cell>
						<uv-cell title="性格" :value="info.want_character"></uv-cell>
						<uv-cell title="其他" :value="info.want_other"></uv-cell>
					</uv-cell-group>
				</view>
			</uv-popup>
		</view>



	</view>
</template>

<script>
	import {
		profileInfo
	} from '../../api/account';
	import {
		objectInfo,
		objectInviteContact,
		AgreeOrRefuseInvite
	} from '../../api/object';
	export default {
		data() {
			return {
				cardCur: 0,
				dotStyle: false,
				info: {},
				invite_status: -1,
				other_invite_status: -1,
				profileEmpty: false,
				modalName: null,
				modalImg: null,
			};
		},


		onLoad(query) {
			// this.TowerSwiper('swiperList');
			// 初始化towerSwiper 传已有的数组名即可
			profileInfo().then(res => {
				if (res == undefined || (res.code != undefined && res.code == 1001)) {
					this.profileEmpty = true
					return
				}
			}).catch(err => {
				console.log(err)
				this.profileEmpty = true
				return
			});

			// 查询资料
			objectInfo({
				account_id: query.id
			}).then(res => {
				if (res.info == undefined) {
					uni.navigateBack()
					return
				}
				this.info = res.info
				this.invite_status = res.invite_status
				this.other_invite_status = res.other_invite_status
				this.info.photos = [this.info.avatar, ...this.info.photos]
			}).catch(err => {
				console.log(err)
				uni.navigateBack()
			});
		},
		methods: {
			cardSwiper(e) {
				this.cardCur = e.detail.current
			},
			showModal(src) {
				this.modalImg = src
				this.modalName = "Image"
			},
			hideModal(e) {
				this.modalName = null
			},
			previewImage(index) {
				uni.previewImage({
					current: index,
					indicator: "none",
					loop: false,
					urls: this.info.photos,
					longPressActions: {},
					sizeType: ['original']
				})
			},
			calculateAge(birthDateStr) {
				var birthDate = new Date(birthDateStr)
				var currentDate = new Date();
				var birthYear = birthDate.getFullYear();
				var birthMonth = birthDate.getMonth();
				var birthDayOfMonth = birthDate.getDate();
				var currentYear = currentDate.getFullYear();
				var currentMonth = currentDate.getMonth();
				var currentDayOfMonth = currentDate.getDate();

				var age = currentYear - birthYear;
				if (currentMonth < birthMonth || (currentMonth == birthMonth && currentDayOfMonth < birthDayOfMonth)) {
					age--;
				}
				return age;
			},
			inviteContact() {
				// if(this.profileEmpty) {
				// 	uni.showToast({
				// 		title: "请完善资料后再发起邀请",
				// 		icon: 'none',
				// 		duration: 2000
				// 	});
				// 	return
				// }

				objectInviteContact({
					account_id: this.info.account_id
				}).then(res => {
					if (res != undefined && res.ok != undefined) {
						uni.showToast({
							title: '邀请成功，耐心等待对方回应',
							icon: 'none'
						});
						this.invite_status = 0
						return
					} else {
						if (res != undefined && res.msg != undefined) {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
							return
						}
						uni.showToast({
							title: '邀请失败',
							icon: 'none'
						});
						return
					}
				}).catch(err => {
					console.log(err)
				});
			},
			acceptContact() {
				AgreeOrRefuseInvite({
					account_id: this.info.account_id,
					status: 1
				}).then(res => {
					if (res != undefined && res.msg != undefined) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
						return
					}
					this.other_invite_status = 1
					uni.showToast({
						title: '已结缘分, 请查看对方联系',
						icon: 'none'
					});
					return
				}).catch(err => {
					console.log(err)
				});
			},
			refuseContact() {
				AgreeOrRefuseInvite({
					account_id: this.info.account_id,
					status: 0
				}).then(res => {
					if (res != undefined && res.msg != undefined) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
						return
					}
					this.other_invite_status = 2
					uni.showToast({
						title: '已拒绝邀请',
						icon: 'none'
					});
					return
				}).catch(err => {
					console.log(err)
				});
			},
			toCheck() {
				this.$refs.popup.open();
				// console.log(this.info)


			},
			handleChange() {

			}
		}
	}
</script>

<style scoped>
	.popup {
		width: 380px;
		padding: 20px 20px 40px;
	}
</style>